package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.Agreement;
import com.api.syh56.shengyunhui.entity.dto.AgreementDto;
import com.api.syh56.shengyunhui.entity.dto.AgreementKDTO;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.AgreementMapper;
import com.api.syh56.shengyunhui.service.AdminAgreementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AdminAgreementServiceImpl extends ServiceImpl<AgreementMapper, Agreement> implements AdminAgreementService {

    @Autowired
    private AgreementMapper agreementMapper;
    @Override
    public void insert(Agreement agreement) {
        agreementMapper.insert(agreement);
    }

    @Override
    public Object selectById(Integer id) {
        QueryWrapper<Agreement> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Agreement::getId,id);
        return agreementMapper.selectOne(wrapper);
    }

    @Override
    public void updateBy(Map<String,String> map) {
        Set<String> keys = map.keySet();
        for (String key : keys){
            QueryWrapper<Agreement> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Agreement::getKeyword,key);
            Agreement agreement = agreementMapper.selectOne(wrapper);
            agreement.setText(map.get(key).substring(3,map.get(key).length()-4));
            agreementMapper.updateById(agreement);
        }
    }




    @Override
    public void delete(Integer id) {
        agreementMapper.deleteById(id);
    }

    @Override
    public List<Agreement> select() {
        List<Agreement> agreements = agreementMapper.selectList(null);
        return agreements;
    }

    @Override
    public Agreement selectByKeyword(String keyword) {
        QueryWrapper<Agreement> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Agreement::getKeyword,keyword);
        Agreement agreement = agreementMapper.selectOne(wrapper);
        return agreement;
    }

    @Override
    public void updateByKey(AgreementKDTO agreementKDTO) {
        QueryWrapper<Agreement> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Agreement::getKeyword,agreementKDTO.getKeyword());
        Agreement agreement = agreementMapper.selectOne(wrapper);
        agreement.setText(agreementKDTO.getText());
        agreementMapper.updateById(agreement);
    }
}
